﻿namespace DesignModeConsole.结构型.组合模式;

/// <summary>
/// 组合中对象声明接口
/// 并且实现所有类公有接口的默认行为
/// </summary>
internal abstract class Compoent
{
    protected string Name;

    public Compoent(string name)
    {
        Name = name;
    }

    /// <summary>
    /// 添加树叶 树枝
    /// </summary>
    /// <param name="compose"></param>
    public abstract void Add(Compoent compose);

    /// <summary>
    /// 移除树叶 树枝
    /// </summary>
    /// <param name="compose"></param>
    public abstract void Remove(Compoent compose);

    /// <summary>
    /// 显示层级
    /// </summary>
    /// <param name="depth"></param>
    public abstract void Display(int depth);
}

internal class Leaf : Compoent
{
    public Leaf(string name) : base(name)
    {
    }

    public override void Add(Compoent compose)
    {
        Console.WriteLine("不能添加叶子节点");
    }

    public override void Display(int depth)
    {
        Console.WriteLine(new string('-', depth) + Name);
    }

    public override void Remove(Compoent compose)
    {
        Console.WriteLine("不能够移除叶子节点");
    }
}

/// <summary>
/// Composite定义树枝的行为，用来存储子部件
/// </summary>
internal class Composite : Compoent
{
    private List<Compoent> Children = new List<Compoent>();

    public Composite(string name) : base(name)
    {
    }

    public override void Add(Compoent compose)
    {
        Children.Add(compose);
    }

    public override void Display(int depth)
    {
        Console.WriteLine(new string('-', depth) + Name);
        foreach (var item in Children)
        {
            item.Display(depth + 2);
        }
    }

    public override void Remove(Compoent compose)
    {
        Children.Remove(compose);
    }
}